/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.bing.email;

import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

/**
 * 纯文本邮件服务类.
 *
 * @author calvin
 */
public class SimpleMailService {

  private static Logger logger = LoggerFactory.getLogger(SimpleMailService.class);

  private JavaMailSender mailSender;
  private String textTemplate;

  /**
   * 发送纯文本的用户修改通知邮件.
   */
  public void sendNotificationMail(String userName) {
    SimpleMailMessage msg = new SimpleMailMessage();
    msg.setFrom("springside3.demo@gmail.com");// smtp.163.com
    msg.setTo("springside3.demo@gmail.com");

    // msg.setFrom("18502107032@163.com");
    // msg.setTo("18502107032@163.com");
    msg.setSubject("用户修改通知");

    // 将用户名与当期日期格式化到邮件内容的字符串模板
    String content = String.format(textTemplate, userName, new Date());
    msg.setText(content);

    try {
      mailSender.send(msg);
      if (logger.isInfoEnabled()) {
        logger.info("纯文本邮件已发送至{}", StringUtils.join(msg.getTo(), ","));
      }
    } catch (Exception e) {
      logger.error("发送邮件失败", e);
    }
  }

  /**
   * Spring的MailSender.
   */
  public void setMailSender(JavaMailSender mailSender) {
    this.mailSender = mailSender;
  }

  /**
   * 邮件内容的字符串模板.
   */
  public void setTextTemplate(String textTemplate) {
    this.textTemplate = textTemplate;
  }
}
